package com.wythe.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wythe.mall.R;
import com.wythe.mall.model.StoreBean;

import java.util.ArrayList;
import java.util.List;

public class DeliveryStoreAdapter extends RecyclerView.Adapter<DeliveryStoreAdapter.ViewHolder> implements Filterable {
    private OnItemClickListener onItemClickListener;
    private Context mContext;
    private List<StoreBean> mList;
    private List<StoreBean> mFilterList = new ArrayList<>();

    private void appendList(List<StoreBean> list) {
        mList = list;
        //这里需要初始化filterList
        mFilterList = list;
    }

    public DeliveryStoreAdapter(Context context,List<StoreBean> beanList) {
        mContext = context;
        mList = beanList;
        mFilterList = beanList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_delivery_store, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final int positionInt = position;
        final StoreBean bean = mFilterList.get(position);
        holder.mName.setText(bean.getTitle());
        holder.mDistance.setText("距您 "+bean.getDistance()+"km");
        holder.mPhone.setText("电话：" + bean.getServicePhone());
        holder.mAddress.setText("地址："+bean.getProvince()+bean.getCity()+bean.getDistrice()+bean.getAddress());
        Glide.with(mContext)
                .load(bean.getShopPhoto())
                .dontAnimate()
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.mCover);
        holder.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(position,bean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mFilterList.size();
    }



    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            //执行过滤操作
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = mList;
                } else {
                    List<StoreBean> filteredList = new ArrayList<>();
                    for (StoreBean str : mList) {
                        //这里根据需求，添加匹配规则
                        if (str.getTitle().contains(charString) || str.getAddress().contains(charString)) {
                            filteredList.add(str);
                        }
                    }

                    mFilterList = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = mFilterList;
                return filterResults;
            }
            //把过滤后的值返回出来
            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                mFilterList = (ArrayList<StoreBean>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

    public interface OnItemClickListener {

        void onClick(int position, StoreBean bean);

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mCover;
        private TextView mName,mDistance,mPhone,mButton,mAddress;

        public ViewHolder(View itemView) {
            super(itemView);
            mCover = itemView.findViewById(R.id.iv_store);
            mName = itemView.findViewById(R.id.tv_name);
            mDistance = itemView.findViewById(R.id.tv_distance);
            mPhone = itemView.findViewById(R.id.tv_phone);
            mButton = itemView.findViewById(R.id.tv_button);
            mAddress = itemView.findViewById(R.id.tv_address);
        }
    }
}
